package com.atfusu.controller;

import com.atfusu.base.BaseController;
import com.atfusu.bean.entity.Role;
import com.atfusu.service.PermissionService;
import com.atfusu.service.RoleService;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.PreparedQuery;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/24
 * Description:
 **/
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private static final String PAGE_INDEX = "role/index";
    private static final String PAGE_CREATE = "role/create";
    private static final String PAGE_EDIT = "role/edit";
    private static final String PAGE_SUCCESS = "common/success";

    @DubboReference
    private RoleService roleService;

    @DubboReference
    private PermissionService permissionService;

    @RequestMapping
    public String index(Map map, HttpServletRequest request) {
        Map<String, Object> filters = super.getFilters(request);
        PageInfo<Role> page = roleService.findPage(filters);
        //将pageInfo放入请求域
        map.put("page", page);
        map.put("filters", filters);
        return PAGE_INDEX;
    }

    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/create")
    public String create() {

        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(Role role) {
        //接收新增角色数据
        //新增角色数据
        roleService.insert(role);
        //跳转到弹出层的成功页面，再通过弹出层success刷新角色列表
        return PAGE_SUCCESS;
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        //根据要修改的角色id查询角色原有信息
        Role role = roleService.getById(id);
        //放到请求域内
        map.put("role", role);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(Role role) {
        roleService.update(role);
        return PAGE_SUCCESS;
    }

    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        roleService.delete(id);
        return "redirect:/role";
    }

    @PreAuthorize("hasAuthority('role.assign')")
    @RequestMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable Long roleId, Map map) {

        List<Map<String, Object>> zNodes = permissionService.findPermissionByRoleId(roleId);
        map.put("zNodes", zNodes);
        map.put("roleId", roleId);
        return "role/assignShow";
    }

    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId, Long[] permissionIds) {
        permissionService.addPermissionRoleRelationShip(roleId,permissionIds);
        return "common/success";
    }
}
